/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/
#ifndef TREEITEM_H
#define TREEITEM_H

/*===========================================================================*\
**      Include Dateien
\*===========================================================================*/
#include <QList>
#include <QVariant>
#include <QDateTime>
#include <QObject>
#include <QIcon>

class QDataStream;
/*===========================================================================*\
**      Deklaration Klasse "TreeItem"
\*===========================================================================*/
class TreeItem : public QObject
{
 Q_OBJECT

 public:
    enum eCol {
        COL_NAME = 0,
        COL_TYPE,
        COL_COUNT
    };

    typedef enum eType {
        ResourceItem,
        NodeItem,
        Invalid
    } Type;

    typedef enum eState {
        Collapsed = 0,
        Expanded
    } IconState;

    typedef struct TreeItemData_ {
      QString name;
      QString text;
      QString shortDescr;
      QString comment;
      QIcon   icon;
      Type    itemType;
      qint32  resourceType;  // SWAMP-Resource-Type
      QString resourceUrl;   // SWAMP-Resource-Name
      TreeItemData_() : itemType(Invalid) {}
    } TreeItemData;

   TreeItem(const TreeItemData &data, QObject  *parent = 0);
   TreeItem(const TreeItemData &data, TreeItem *parent = 0);
   TreeItem(const QString &dirName, TreeItem *parent = 0);
   TreeItem(TreeItem *parent = 0);
   virtual ~TreeItem();

   virtual void         serialize(QDataStream*) const;

   void                 appendChild(TreeItem *child);
   void                 removeChild(TreeItem *child);
   void                 selfDestroy();

   TreeItem            *child(int row);
   TreeItem            *takeChild(int index);
   int                  childCount()     const;
   int                  columnCount()    const;
   QVariant             data(int column) const;
   QString              toolTip()        const;
   int                  relativeRow()    const;
   int                  absoluteRow()    const;
   QString              path(const QString &childPath = "") const;
   QIcon                icon() const;

   TreeItem            *parent() const;
   TreeItem            *childByName(const QString &name) const;
   TreeItem            *childByPath(const QString &path) const;

   inline void          touch() { mAnimationTime = 255; }
   inline QColor        textColor() const { return mTextColor; }
   inline void          setParentItem(TreeItem *parentItem) { mParentItem = parentItem; }
   inline void          setName(const QString &name)        { mData.name = name;        }
   inline void          setResource(const QString &url, qint32 type)
                             { mData.resourceUrl = url; 
                               mData.resourceType = type; }
   inline QString       name() const { return mData.name; }
   inline QString       text() const { return mData.text; }
   inline QString       shortDescr() const { return mData.shortDescr; }
   inline QString       comment() const { return mData.comment; }
   inline void          setType(Type t) { mData.itemType = t; }
   inline Type          type() const { return mData.itemType; }
   inline QString       resourceUrl() const { return mData.resourceUrl; }
   inline qint32        resourceType() const{ return mData.resourceType; }

   inline bool          isDir() const { return mData.itemType==NodeItem; }
   inline void          setState(IconState state) { mState = state; }
   inline bool          isRoot() const { return (mParentItem==NULL); }
   inline void          setParentWidget(QWidget *widget) { mViewWidget=widget; }

 signals:
   void changed(TreeItem *item);

 public slots:
   void                 deleteAction();
   void                 animate();

 protected:
   TreeItemData         mData;

 private:
   TreeItem            *itemByPathParts(QStringList &pathParts) const;
   void                 initIcons();
   void                 deleteChildren();

   QList<TreeItem*> mChildItems;
   TreeItem        *mParentItem;
   IconState        mState;
   QIcon            mExpIcon;
   QIcon            mColIcon;
   QWidget         *mViewWidget;
   QColor           mTextColor;
   int              mAnimationTime;
};

#endif //TREEITEM_H
